package com.example.finallyjob.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.finallyjob.dao.ActivityImp;
import com.example.finallyjob.entity.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class OpenArticle {
    Logger logger= LoggerFactory.getLogger(getClass());
    @Autowired
    ActivityImp activityImp;
    @Autowired
    MongoTemplate mongoTemplate;

    @ApiOperation(value = "读取作品和作者信息",notes = "返回作品信息和作者信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "作品文件的编号", required = true, dataType = "String"),
    })
    @Transactional(readOnly = true)
    public ArticleInfo searchInfo(String num){
        Article article=new Article();
        article.setArticleNum(num);
        article.setType("text");
        ArticleInfo article1=activityImp.AdminArtivleInfo(article);
        if (article1 == null) {
            article1=activityImp.UserArtivleInfo(article);
        }
        return article1;
    }

    @ApiOperation(value = "读取视频和作者信息",notes = "返回视频信息和作者信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "作品文件的编号", required = true, dataType = "String"),
    })
    @Transactional(readOnly = true)
    public ArticleInfo videoInfo(String num){
        Article article=new Article();
        article.setArticleNum(num);
        article.setType("video");
        ArticleInfo article1=activityImp.AdminArtivleInfo(article);
        if (article1 == null) {
            article1=activityImp.UserArtivleInfo(article);
        }
        return article1;
    }


    @ApiOperation(value = "读取作品评论和回复",notes = "返回回复和回复的评论")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "作品文件的编号", required = true, dataType = "String"),
    })
    public List<Reply> LoadComments(String num){
        Query query=new Query(new Criteria("article_num").is(num));
        List<Reply> lists = mongoTemplate.find(query, Reply.class);
        for(Reply reply: lists){
            String comment_list = reply.getComment_list();
            System.out.println(comment_list);
            if(comment_list != null && !(comment_list.equals("")))
            {
                JSONArray jsonArray = JSON.parseArray(comment_list);
                Iterator<Object> iterator = jsonArray.iterator();
                List<String> id = new ArrayList<>();
                while (iterator.hasNext()) {
                    id.add((String) iterator.next());
                }
                Query query1=new Query();
                query1.addCriteria(new Criteria("comment_id").in(id));
                reply.setComments(mongoTemplate.find(query1, Comments.class));
            }
        }
        return lists;
    }

    public StringBuilder openArtcle(String num) throws IOException {
        String s;
        File file=new File(FileUnit.textUrl+num+".txt");
        BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
        StringBuilder stringBuilder=new StringBuilder();
        while((s=bufferedReader.readLine()) != null){
            stringBuilder.append(s);
        }
        return stringBuilder;
    }

}
